package com.base.mail.bean;

import android.support.annotation.NonNull;
import android.text.TextUtils;


import com.base.mail.utils.ContractUtils;

import java.io.Serializable;
import java.text.Collator;
import java.util.ArrayList;

/**
 * Created by jp on 2018/1/23.
 */

public class ContactEntity implements Comparable<ContactEntity>,Serializable {

    private static final Collator collator= Collator.getInstance();
    private String userid;
    private String realname;
    private String account;


    private String shortname;
    private String firstChar="";
    private String nameSpell;
    private boolean isFirstCharEnglish;
    private String[] nameSpells;
    private boolean isChecked;
    private String[] shortNames;
    private boolean isInner;
    private int avatar_id;

    public ContactEntity() {
    }

    public ContactEntity(String userid, String realname) {
        this.userid = userid;
        this.realname = realname;
        generateSpell();
    }
    public ContactEntity(String userid, String realname, String account) {
        this.userid = userid;
        this.realname = realname;
        this.account=account;
        generateSpell();
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getShortname() {
        return shortname;
    }

    public void setShortname(String shortname) {
        this.shortname = shortname;
    }

    public String getFirstChar() {
        return firstChar;
    }

    public void setFirstChar(String firstChar) {
        this.firstChar = firstChar;
    }

    public String getNameSpell() {
        return nameSpell;
    }

    public void setNameSpell(String nameSpell) {
        this.nameSpell = nameSpell;
    }

    public boolean isFirstCharEnglish() {
        return isFirstCharEnglish;
    }

    public void setFirstCharEnglish(boolean firstCharEnglish) {
        isFirstCharEnglish = firstCharEnglish;
    }

    public String[] getNameSpells() {
        return nameSpells;
    }

    public void setNameSpells(String[] nameSpells) {
        this.nameSpells = nameSpells;
    }

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    public String[] getShortNames() {
        return shortNames;
    }

    public void setShortNames(String[] shortNames) {
        this.shortNames = shortNames;
    }

    public boolean isInner() {
        return isInner;
    }

    public void setInner(boolean inner) {
        isInner = inner;
    }

    public int getAvatar_id() {
        return avatar_id;
    }

    public void setAvatar_id(int avatar_id) {
        this.avatar_id = avatar_id;
    }

    public void generateSpell() {
        String name=null;
//        if (TextUtils.isEmpty(this.getAccount()))
//        {
//             name = this.getRealname() ;
//        }else
//        {
//             name = this.getRealname()+ this.getAccount();
//        }
        name = this.getRealname() ;

        if(!TextUtils.isEmpty(name)) {
            ArrayList<String> shortNamesList = new ArrayList();
            ArrayList<String> nameSpellsList = new ArrayList();
            int length = name.length();

            for(int i = 0; i < length; ++i) {
//                String pingy = ContractUtils.getPinyin(name);
//                ArrayList<String> pinyins = ContractUtils.findPinyin(name.charAt(i));
                ArrayList<String> pinyins = new ArrayList<>();
                pinyins.add(ContractUtils.getPinyin(name));
                if(pinyins != null && pinyins.size() > 0) {
                    int snSize = shortNamesList.size();
                    ArrayList<String> shortNamescache = new ArrayList();
                    ArrayList<String> nameSpellscache = new ArrayList();
                    int j = 0;

                    for(int size = pinyins.size(); j < size; ++j) {
                        String pinyin = (String)pinyins.get(j);
                        if(pinyin != null) {
                            if(snSize == 0) {
                                shortNamescache.add(String.valueOf(pinyin.charAt(0)));
                                nameSpellscache.add(pinyin);
                            } else {
                                for(int k = 0; k < snSize; ++k) {
                                    try {
                                        shortNamescache.add((String)shortNamesList.get(k) + String.valueOf(pinyin.charAt(0)));
                                        nameSpellscache.add((String)nameSpellsList.get(k) + pinyin);
                                    } catch (OutOfMemoryError var17) {
                                        ;
                                    }
                                }
                            }
                        }
                    }

                    if(shortNamescache.size() > 0) {
                        shortNamesList.clear();
                        shortNamesList.addAll(shortNamescache);
                        nameSpellsList.clear();
                        nameSpellsList.addAll(nameSpellscache);
                    }
                }
            }

            this.nameSpells = (String[])nameSpellsList.toArray(new String[0]);

            try {
                this.nameSpell = TextUtils.join("\r", this.nameSpells);
            } catch (OutOfMemoryError var16) {
                ;
            }

            this.shortNames = (String[])shortNamesList.toArray(new String[0]);

            try {
                this.shortname = TextUtils.join("\r", this.shortNames);
            } catch (OutOfMemoryError var15) {
                var15.printStackTrace();
            }

            if(this.shortNames.length > 0 && !TextUtils.isEmpty(this.shortNames[0])) {
                char c = this.shortNames[0].charAt(0);
                this.isFirstCharEnglish = ContractUtils.isEnglishOnly(c);
//                this.firstChar = String.valueOf(c);
                this.firstChar = ContractUtils.getPinyin(name).substring(0,1);
            }
        }

    }


    @Override
    public int compareTo(@NonNull ContactEntity another) {
        if(another == null) {
            return -1;
        } else {
            boolean isEng1 = this.isFirstCharEnglish();
            boolean isEng2 = another.isFirstCharEnglish();
            if(!isEng1) {
                if(isEng2) {
                    return 1;
                }
            } else if(!isEng2) {
                return -1;
            }

            int code = collator.compare(this.getFirstChar(), another.getFirstChar());
            return code == 0?(this.getRealname() == null?(another.getRealname() == null?0:1):(another.getRealname() == null?-1:collator.compare(this.getRealname(), another.getRealname()))):code;
        }
    }


    @Override
    public boolean equals(Object obj) {
        return TextUtils.equals(this.getUserid(),((ContactEntity)obj).getUserid());
    }
}
